﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Scheduler.Presentation_Layer
{
    /**Class: WeekDisplayer
     * @author: Hieu Nhan (Kevin)
     * @version: 1.0
     * Course: ITEC 4860 Spring, 2012
     * Written: March XX, 2012
     * This class - Displays the information from the Shift table in the database using a DataGridView
     * Purpose: Display all the shifts for a given week.
     * NOTE: This window is a variation of Sally's EmployeeList and will only be used for now
     * as a placeholder just to have something to show/test
     * THIS IS A PLACEHOLDER WINDOW AND WILL BE SEEING DRAMATIC CHANGE SOON TO BETTER REPRESENT WHAT THE GROUP WANTS
     */

    public partial class WeekDisplayer : Form
    {
        // ArrayList of shifts
        private ArrayList shiftList;
        // ArrayList of all shifts
        private ArrayList allShifts;
        // DateTime used to represent a speciifc workday
        private DateTime scheduleDate;
        // The Company ID that scheduler will be used for
        private string companyID;
        // The employee's user UD
        private string employeeUserID;
        // Data Access Layer for the database and shift table
        private Scheduler.Data_Access_Layer.ScheduleDisplayer shiftData;

        /**Method: WeekDisplayer constructor
         * Initializes the two ArrayLists and also creates and dictates
         * how the DataGridView will look
         */

        public WeekDisplayer()
        {
            InitializeComponent();
            shiftList = new ArrayList();
            allShifts = new ArrayList();
            createList();
            displayList();
        }

        /**Method: createList
         * Gets all shifts from the database
         * and adds it to the allShifts arraylist
         * For each arraylist in the allShifts arraylist
         * get the information held at the proper index and
         * add add/store it in the shiftList arraylist
         */
 
        private void createList()
        {
            shiftData = new Scheduler.Data_Access_Layer.ScheduleDisplayer(scheduleDate, companyID, employeeUserID);
            allShifts = shiftData.getAllShifts();
            foreach (ArrayList shifts in allShifts)
            {
                scheduleDate = (DateTime)shifts[0];
                employeeUserID = (string)shifts[1].ToString();
                TimeSpan startTime = (TimeSpan)shifts[2];
                TimeSpan endTime = (TimeSpan)shifts[3];
                Boolean isReleased = (Boolean)shifts[4];
                shiftList.Add(new ListableShifts(scheduleDate, employeeUserID, startTime, endTime, isReleased));
            }
        }

        /**Class: ListableShifts
         * @author: Hieu Nhan (Kevin)
         * @version: 1.0
         * Course: ITEC 4860 Spring, 2012
         * Written: March XX, 2012
         * This class - stores all the information of a shift
         * Purpose: allow for the shift information to be used outside out of the database
         */

        class ListableShifts
        {
            public DateTime scheduleDate { get; set; }
            public string employeeUserID { get; set; }
            public TimeSpan startTime { get; set; }
            public TimeSpan endTime { get; set; }
            public Boolean isReleased { get; set; }

            /**Method: ListableShifts constructor
             * sets the variables used to those entered into the parameter
             * @param listScheduleDate the date for the shift
             * @param listEmployeeUserID the employee's user ID
             * @param listStartTime the start time of the employee's shift
             * @param listEndTime the end time of the employee's shift
             * @param listIsReleased the state of the employee's shift, whether true or false
             */

            public ListableShifts(DateTime listScheduleDate, string listEmployeeUserID, TimeSpan listStartTime, TimeSpan listEndTime, Boolean listIsReleased)
            {
                scheduleDate = listScheduleDate;
                employeeUserID = listEmployeeUserID;
                startTime = listStartTime;
                endTime = listEndTime;
                isReleased = listIsReleased;
            }
        }

        /**Method: displayList
         * Adds the information from the the shiftList from the formatted list
         * to the ListBox
         */

        private void displayList()
        {
            ShiftListBox.BeginUpdate();
            foreach (ListableShifts listShifts in shiftList)
            {
                ShiftListBox.Items.Add(formatField(listShifts));
            }
            ShiftListBox.EndUpdate();
        }

        /**Method: formatField
         * Takes the information of the shift from the ListableShift class
         * and returns it all in a string that's formatted to look more pleasing
         * @param ListableShifts to have access to the information from the database
         * @return string with all the information of a shift in a more pleasing format
         */

        private string formatField(ListableShifts ls)
        {
            string newRow;
            ListableShifts listShift = ls;
            DateTime listSchedDate = listShift.scheduleDate;
            string listEmployeeUserID = listShift.employeeUserID;
            TimeSpan listStartTime = listShift.startTime;
            TimeSpan listEndTime = listShift.endTime;
            Boolean listIsReleased = listShift.isReleased;
            newRow = listSchedDate.ToString().PadRight(20) + "       " + listEmployeeUserID.ToString().PadRight(15) + " "
                + listStartTime.ToString().PadRight(20) + " " + listEndTime.ToString().PadRight(20) + " "
                + listIsReleased.ToString().PadRight(20) + "\n";
            return newRow;
        }

        /**Method: returnButton_Click
         * Closes the window
         */

        private void returnButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
